<?php

namespace App\Http\Controllers\Api\V1\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Models\PayConfig;

class OrderResource extends JsonResource
{
	public function toArray($request)
	{
		$resource = $this->resource;
		
		$payDesc="";
		$payStatus="";
		$payTimes="";
		$expiredAt ="";
		
		if($resource->pay_config_id){
			$payConfig = PayConfig::find($resource->pay_config_id);
			$payDesc = $payConfig->desc;
			$payStatus = $payConfig->status;
			$payTimes = $payConfig->num;
			if($payStatus=="vip" && !empty($resource->paid_at)){

				$expiredAt=(string)$resource->paid_at->addDays($payConfig->num);
			}

		}
		return [
			'id'                        => (int)$resource->id,
			'user_id'                   => (int)$resource->user_id,
			'type'                      => $resource->type ?? '',
			'number'                    => $resource->number,
			'total_amount'              => $resource->total_amount,
			'is_paid'                   => $resource->isPaid(),
			'is_closed'                 => $resource->isClosed(),
			'is_cancelled'              => $resource->isCancelled(),
			'has_refund'                => (bool)$resource->has_refund,
			'paid_method'               =>$resource->paid_method,
			'created_at'                => (string)$resource->created_at,
			'paid_at'                => (string)$resource->paid_at,
			'pay_desc'				 =>$payDesc,
			'pay_status'				 =>$payStatus,
			'pay_times'				 =>$payTimes,
			'expired_at'			 =>$expiredAt,
			'user'                      => new UserResource($this->whenLoaded('user')),
		];
	}
}
